package com.geekathon.mobzilla.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.geekathon.mobzilla.model.User;
import com.geekathon.mobzilla.util.LoadBalancingStatistics;
import com.google.gson.Gson;

/**
 * Servlet implementation class LoadBalancingStats
 */
@WebServlet("/LoadBalancingStats")
public class LoadBalancingStats extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadBalancingStats() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String number = request.getParameter("count");
		int count = 0;
		if(number != null){
			count = Integer.parseInt(number);
			 try {
				List<User> users = LoadBalancingStatistics.getTopUsers(count);
				Gson gson = new Gson();
				String jsonString = gson.toJson(users);
				System.out.println(jsonString);
				response.getWriter().print(jsonString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		else {
			response.getWriter().print("count character not found");
		}
		
	}

}
